package com.longtop.perform.zb.system.web;

import com.longtop.framework.base.BaseAction;
import com.longtop.framework.common.QueryConditions;
import com.longtop.framework.common.QueryResult;
import com.longtop.framework.security.BaseCheckedException;
import com.longtop.framework.util.BeanUtil;
import com.longtop.framework.util.WebUtil;
import com.longtop.intelliweb.action.IActionContext;
import com.longtop.intelliweb.action.IInitContext;
import com.longtop.intelliweb.view.Dataset;
import com.longtop.perform.zb.system.service.SystemClassService;
import com.longtop.perform.zb.system.service.SystemService;

/**
 * 
 * @author 杨向阳
 * 
 */
public class SystemClassListAction extends BaseAction {

	private final static String DATASET_SYSTEM = "Dataset_System";
	private final static String DATASET_SYSTEMCLASS = "Dateset_SystemClass";

	/**
	 * 系统共用：初始化调用
	 */
	public void init(IInitContext context) {
		super.init(context);
	}

	/**
	 * 本页jsp：初始化业务数据(覆写父类方法)
	 */
	public void thisPageInit(IInitContext context) {

		String systemId = context.getRequest().getParameter("systemId");

		// 调用Spring的业务逻辑类
		Dataset zbSystemDataset = null; // dataset数据集
		SystemService systemService = getBean(SystemService.BEAN_ID);
		SystemClassService systemClassService = getBean(SystemClassService.BEAN_ID);
		// 获取页面上的dataset的结构
		zbSystemDataset = (Dataset) context.getDataset(DATASET_SYSTEM);

		Dataset zbSystemClassDataset = (Dataset) context.getDataset(DATASET_SYSTEMCLASS);
		// 验证数据集对象是否存在
		if (!WebUtil.validateDatasetExist(this, zbSystemDataset, DATASET_SYSTEM)) {
			return;
		}
		int pageSize = zbSystemDataset.getPageSize();
		// 设置定性
		QueryConditions queryConditions = WebUtil.createQueryConditions();
		queryConditions.setCondition("systemId", QueryConditions.EQUAL, systemId);
		QueryResult queryResult = systemService.loadSystem(queryConditions, 1, pageSize);

		QueryConditions classQueryConditions = WebUtil.createQueryConditions();
		classQueryConditions.setCondition("systemId", QueryConditions.EQUAL, systemId);
		QueryResult classQueryResult = systemClassService.loadSystemClass(classQueryConditions, 1, -1);
		try {
			BeanUtil.queryResultToDataset(queryResult, zbSystemDataset, false, true);
			BeanUtil.queryResultToDataset(classQueryResult, zbSystemClassDataset, false, true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 删除分类
	 * 
	 * @param context
	 */
	public void deleteClass(IActionContext context) {
		// 得到参数用户Id
		String classId = context.getCommandParameterValue("classId");

		// SessionObject对象
		// SessionObject sessionObject = this.getSessionObject(context);

		// 调用Spring的业务逻辑类
		SystemClassService systemClassService = getBean(SystemClassService.BEAN_ID);

		try {
			systemClassService.deleteSystemClass(classId);
		} catch (BaseCheckedException e) {
			this.hintErrorMessage(e.getMessage());
		}
	}
}